package org.webapp.managedBeans;

import java.io.Serializable;
import java.io.IOException;
import javax.faces.event.ActionEvent;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import org.ejb.api.UtilisateurInterface;
import org.ejb.entities.Utilisateur;
import java.util.logging.Logger;


@SessionScoped
@ManagedBean
public class LoginMB implements Serializable{
	
    /**
	 * 
	 */
	private static final long serialVersionUID = 3234189748156662874L;
	private String username;      
    private String password;
    private Utilisateur user=null;
    @EJB
    private UtilisateurInterface ui;
      
    public String getUsername() {  
        return username;  
    }  
  
    public void setUsername(String username) {  
        this.username = username;  
    }  
  
    public String getPassword() {  
        return password;  
    }  
  
    public void setPassword(String password) {  
        this.password = password;  
    }
  
  public Utilisateur getUser() {
		return user;
	}

	public void setUser(Utilisateur user) {
		this.user = user;
	}

	public void login() {
  
      FacesMessage msg = null;  
          
      user=this.ui.authenticate(this.username, this.password);
      
      if(user!=null) {
    	  HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
    	  session.setAttribute(user.getId()+"", user);
    	  
          msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Bienvenue", user.getFirstname());  
      } else {   
          msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Login Error", "identifications invalides");  
      }  
        
      FacesContext.getCurrentInstance().addMessage(null, msg);  
  }
	
  public void loginC(ActionEvent a) {
	  
      FacesMessage msg = null;  
      System.out.println("valider !!");
      user=this.ui.authenticate(this.username, this.password);
      
      if(user!=null) {
    	  HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
    	  session.setAttribute(user.getId()+"", user);
    	  
          msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Bienvenue", user.getFirstname()); 
      } else {   
          msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Login Error", "identifications invalides");  
      }   
       
      if (user != null){
    	  doRedirect("carteCredit.xhtml");
    	  System.out.println("valider");
      }
      
      FacesContext.getCurrentInstance().addMessage(null, msg);
    }
	
	private void doRedirect(String url){  
		try {  
			FacesContext context=FacesContext.getCurrentInstance();  
			context.getExternalContext().redirect(url);  
		} catch (IOException e){  
			e.printStackTrace();  
		}  
	}
	
	public void valider(ActionEvent a){
		if(user == null){
			doRedirect("choixConnexion.xhtml");
		}else{
			doRedirect("carteCredit.xhtml");
		}
		FacesMessage msg = new FacesMessage("vous avez valide votre panier. Nous allons passer au paiement" );  
		FacesContext.getCurrentInstance().addMessage(null, msg);
	}
}
